﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Security;
using System.Web.Profile;

using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;
using System.Data;

namespace WebSite.admin
{
    public partial class login : System.Web.UI.Page
    {

        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                Session.Clear();
                if (Request.QueryString["msg"] == "otherlogin")
                {
                    this.divMessage.InnerHtml = "该账号已在其它地方登录，您被迫下线！";
                    
                    return;
                }
                userlogin();
                string sSource = fun.geturl("");

            }
        }

   


        void userlogin()
        {
            if (string.IsNullOrEmpty(Request.QueryString["calluser"]) && this.txtUserName.Text == "")
            {
                return;
            }
            this.Session["Adminlogin"] = null;
            Session["memberid"] = null;
            Session["username"] = null;
            Session["rankid"] = null;
            Session["CompanyId"] = null;
            Session["AdminTypeId"] = null;

            string usn = string.IsNullOrEmpty(Request.QueryString["calluser"]) ? re.replace(this.txtUserName.Text) : Request.QueryString["calluser"];
            string pwd = string.IsNullOrEmpty(Request.QueryString["callpas"]) ? FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtPassowrd.Text.Trim()), "MD5") : Request.QueryString["callpas"];
            if (WebSite.login.lo(usn, pwd))
            {
                this.Session["Adminlogin"] = usn;
                try
                {
                    this.Session["inphoneNumber"] = theAdminLogin.GetInphoneNumberByName(usn);
                }
                catch { }
                Session["calluser"] = Request.QueryString["calluser"];
                Session["callpas"] = Request.QueryString["callpas"];
                Session["callerid"] = Request.QueryString["callerid"];

                // ExtAspNet.Alert.Show("登录成功", "", "window.location.href='/admin/index.aspx';");
                // Server.Transfer("index.aspx");
                Response.Redirect("index.aspx");
            }
            else
            {
                this.divMessage.InnerHtml = "登录名或者登录密码不正确！";
                return;
            }
      
            this.Session.Timeout = 60;
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {

            this.divMessage.InnerHtml = "";

            this.Session["Adminlogin"] = null;
            Session["memberid"] = null;
            Session["username"] = null;
            Session["rankid"] = null;
            Session["CompanyId"] = null;
            Session["AdminTypeId"] = null;
            //if (base.Request.Cookies["CheckCode"] == null)
            //{
            //    base.Response.Write("<script language=javascript>alert('您的浏览器设置已被禁用Cookies,您必须设置浏览器允许使用Cookies选项后才能使用本系统。')</script>");
            //}
            if ((Session["checkCode"] == null || string.Compare(Session["checkCode"].ToString().ToUpper(), this.txtValidate.Text.ToUpper(), true) != 0))
            {
                //this.Image1.ImageUrl = "~/admin/CheckCode.aspx?time=" + DateTime.Now.Ticks.ToString();
                //ExtAspNet.Alert.Show("验证码错误，请重新输入！");
                this.divMessage.InnerHtml = "验证码错误，请重新输入！";
              
                return;
                // base.Response.Write("<script language=javascript>alert('验证码错误，请重新输入！');window.location.href='login.aspx';</script>");
            }
            else
            {
                userlogin();
            }
        }

    }
}